package com.sh.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sh.common.BaseResult;
import com.sh.common.enums.BizCodeEnum;
import com.sh.common.enums.PublishedEnum;
import com.sh.common.pojo.Contants;
import com.sh.common.pojo.bo.AlgoSaveBO;
import com.sh.common.pojo.bo.PutObjectRes;
import com.sh.common.pojo.dto.AlgoDto;
import com.sh.common.pojo.query.AlgoPageQuery;
import com.sh.common.pojo.query.AlgoParseParam;
import com.sh.common.utils.FileUtil;
import com.sh.entity.Algo;
import com.sh.entity.AlgoDetail;
import com.sh.exceptions.BizException;
import com.sh.service.IAlgoDetailService;
import com.sh.service.IAlgoService;
import com.sh.service.IOSSService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/algo")
public class AlgoController {

    @Autowired
    private IAlgoService algoService;

    @Autowired
    private IAlgoDetailService algoDetailService;

    @PostMapping("/page")
    public BaseResult page(@Valid @RequestBody AlgoPageQuery query) {
        return BaseResult.success(algoService.queryAlgoPage(query));
    }

    @GetMapping("/{id}")
    public BaseResult getById(@PathVariable("id") Long id) {
        Algo algo = algoService.getById(id);
        if (algo == null) {
            return BaseResult.fail(BizCodeEnum.DATA_NOT_EXISTED);
        }
        AlgoDto algoDto = algoService.transferToDto(algo);
        QueryWrapper<AlgoDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("algo_id", algo.getId());
        AlgoDetail algoDetail = algoDetailService.getOne(queryWrapper);
        if (algoDetail != null) {
            algoDto.setAlgoSchema(algoDetail.getAlgoSchema());
            algoDto.setAlgoSchemaPath(algoDetail.getAlgoSchemaPath());
        }
        return BaseResult.success(algoDto);
    }

}
